package com.myh.cc.coom.search.view

import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.myh.cc.R
import com.myh.cc.base.BaseFragment
import com.myh.cc.coom.search.net.SearchNetControl
import com.myh.cc.coom.search.viewmodel.SearchModel
import com.myh.cc.databinding.FragmentSearchBinding





/**
 * des    ：
 * create    ：  mayahao.
 * date   ：  2018/1/15.   ：
 */
class SearchFragment: BaseFragment<FragmentSearchBinding,SearchNetControl,SearchModel>() {

    override fun getLayoutId(): Int {
        return R.layout.fragment_search
    }

    override fun initView() {
        mBinder.searchRvHot.layoutManager = GridLayoutManager(context, 4)

        mBinder.searchRvResult.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL, false)

        initEditAndBotton()

        mModel.getHot()


    }

    fun initEditAndBotton() {


        mBinder.searchBt.setOnClickListener{view ->
            // 清空
            mBinder.searchEt.setText("")
            mModel.changeRecycleView(1)
        }

        // 确认事件
        mBinder.searchEt.setOnEditorActionListener{
            textView, i, keyEvent ->

            if ((i == 0 || i == 3) && keyEvent != null) {
                mModel.getSearch(mBinder.searchEt.text.toString())
                mModel.changeRecycleView(2)
                true;
            }


            false
        }

        mBinder.searchEt.addTextChangedListener(object :TextWatcher{

            override fun afterTextChanged(p0: Editable) {
                    if (p0.toString().isNotEmpty()) mBinder.searchBt.visibility = View.VISIBLE
                    else  mBinder.searchBt.visibility = View.GONE
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }
        })

    }


}